package com.example.androidmainshixun.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.adapter.CartBean;
import com.example.androidmainshixun.entity.Goods;

import java.util.ArrayList;
import java.util.List;

public class Goods1Activity extends AppCompatActivity {
    private ImageView mImage;
    private Button mButton;
    private CartBean cartBean;
    private Goods mGoods; // 当前商品的实例

    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods1);
        mImage = findViewById(R.id.image1);
        mButton = findViewById(R.id.button);//购物车按钮

// 为加入购物车按钮设置点击事件
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddToCartClick(); // 确保正确调用 onAddToCartClick 方法
            }
        });


        // 假设您已经通过Intent获取了商品数据
        mGoods = getIntent().getParcelableExtra("GOODS_ITEM");
        // 从Intent中获取传递过来的数据
        int position = getIntent().getIntExtra("POSITION", -1); // 获取位置

        mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    // 添加onAddToCartClick方法
    public void onAddToCartClick() {
        if (mGoods != null) {
            addToCart();
        } else {
            Toast.makeText(this, "商品数据为空", Toast.LENGTH_SHORT).show();
        }
    }


    private void addToCart() {
        // 确保 mGoods 不为空
        if (mGoods != null) {
            cartBean = new CartBean(
                    mGoods.getName(),
                    mGoods.getImageResId(), // 确保这是正确的图片资源 ID
                    false,
                    1,
                    mGoods.getPrice()
        );

        // 获取购物车管理器实例
        ShoppingCartManager cartManager = ShoppingCartManager.getInstance();
        // 使用管理器添加商品到购物车
        cartManager.addToCart(cartBean);

        // 显示添加成功的提示
        Toast.makeText(Goods1Activity.this, "商品已添加到购物车", Toast.LENGTH_SHORT).show();
        // 添加日志输出，以确保商品数据被正确添加
            Log.d("CART", "Added item: " + cartBean.toString());
        } else {
            Toast.makeText(Goods1Activity.this, "获取商品数据失败", Toast.LENGTH_SHORT).show();
        }
    }


    public static class ShoppingCartManager {
        private static ShoppingCartManager instance;
        private List<CartBean> cartItems;


        private ShoppingCartManager() {
            cartItems = new ArrayList<>();
        }

        public static synchronized ShoppingCartManager getInstance() {
            if (instance == null) {
                instance = new ShoppingCartManager();
            }
            return instance;
        }

        public void addToCart(CartBean item) {
            cartItems.add(item);
        }
    }
}
